package com.fujitsu.cn.fnst.trigger;

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Scope;

import com.fujitsu.cn.fnst.base.DateUtils;
import com.fujitsu.cn.fnst.base.ThirdVelocityEmailService;
import com.fujitsu.cn.fnst.entiy.MeetingBook;
import com.fujitsu.cn.fnst.entiy.User;
import com.fujitsu.cn.fnst.service.MeetingBookService;
import com.fujitsu.cn.fnst.service.MeetingUseService;
import com.fujitsu.cn.fnst.service.UserService;
@Configurable("dailyTrigger")
@Scope
@DependsOn
public class DailyTrigger implements Job {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DailyTrigger.class);

private UserService userService;
private MeetingBookService meetingBookService;
private MeetingUseService meetingUseService;
private ThirdVelocityEmailService thirdVelocityEmailService;
public UserService getUserService() {
	return userService;
}
public void setUserService(UserService userService) {
	this.userService = userService;
}
public MeetingBookService getMeetingBookService() {
	return meetingBookService;
}
public void setMeetingBookService(MeetingBookService meetingBookService) {
	this.meetingBookService = meetingBookService;
}
public MeetingUseService getMeetingUseService() {
	return meetingUseService;
}
public void setMeetingUseService(MeetingUseService meetingUseService) {
	this.meetingUseService = meetingUseService;
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
	Date now= new Date();
    List<MeetingBook>books=meetingBookService.getAllMeetingsByTime(DateUtils.getFormattedEndTime(now));
    Map<String, Object> map = new HashMap<String, Object>();
    for(MeetingBook book:books){
    	User user=userService.find(book.getUserId());
    	if(user!=null){
    		map.clear();
			map.put("book", book);
			thirdVelocityEmailService.sendEmail(map, "您的会议要开始了，请做好准备！",
					"com/fujitsu/cn/fnst/mailTemplates/begin.vm",
					new String[]{user.getEmail()}, new String[] {});
    	}
    }
    books=meetingBookService.getAllMeetingsByTime(DateUtils.getFormattedStartTime(now));
    for(MeetingBook book:books){
    	User user=userService.find(book.getUserId());
    	if(user!=null){
    		map.clear();
			map.put("book", book);
			meetingBookService.deleteBook(book.getUserId(),book.getId());
			//TODO 增加浪费表中数据
			thirdVelocityEmailService.sendEmail(map, "您的会议被系统自动取消了！",
					"com/fujitsu/cn/fnst/mailTemplates/end.vm",
					new String[]{user.getEmail()}, new String[] {});
    	}
    }
    MDC.put("userId","-1");
	MDC.put("userType", "0");
    logger.info("DailyTrigger 被执行了!");
	
}
public ThirdVelocityEmailService getThirdVelocityEmailService() {
	return thirdVelocityEmailService;
}
public void setThirdVelocityEmailService(ThirdVelocityEmailService thirdVelocityEmailService) {
	this.thirdVelocityEmailService = thirdVelocityEmailService;
}
}
